/**
 * 弹出框类
 * @param title 标题
 * @constructor
 */
function YTATApopout(options) {
    var defaults = {
        title: '',
        showFooter: true,// 是否显示底部按钮
        whetherClose: true,// 是否提供关闭按钮
        emptyClose: false,// 点击空白处关闭
        noPadding: false,// 是否有内边距
        noBackground:false// 是否有背景
    };
    var settings = $.extend(defaults, options);
    for (key in settings){
        this[key] = settings[key];
    }
    this.init();
}

YTATApopout.prototype = {
    /**
     * 初始化
     */
    init: function() {
        var that = this;
        var popoutHtml = '<div id="popout" style="display: none;">';
        popoutHtml += '<div class="uu-mask"></div>';
        popoutHtml += '<div class="uu-popout '+(this.noBackground?'no_background':'')+'">';
        if (this.title) {
            popoutHtml += '<div class="uu-popout-hd"><div class="uu-popout-title">' + this.title + '</div></div>';
        }
        popoutHtml += '<div class="uu-popout-bd '+(this.noPadding?'no_padding':'')+'">';
        //if(this.whetherClose){
        //    popoutHtml += '<div class="closePopout '+(this.title?'small':'big')+'"></div>';
        //}
        popoutHtml += '</div>';
        popoutHtml += '<div class="uu-popout-ft">';
        if(this.showFooter){
            popoutHtml += '<a href="javascript:;" class="uu-popout-btn uu-popout-btn-default"></a>';
            popoutHtml += '<a href="javascript:;" class="uu-popout-btn uu-popout-btn-primary"></a>';
        }
        popoutHtml += '</div>';
        popoutHtml += '</div>';
        popoutHtml += '</div>';
        $('body').append(popoutHtml);

        this.popout = $('#popout');
        if(this.showFooter){
            // 设置默认按钮
            this.cancelBtn = this.getCancelBtn();
            this.primaryBtn = this.getPrimaryBtn();
            this.setBtn(2, ['取消', '确定']);
        }
        if(this.emptyClose){
            $('.uu-mask').unbind("click").on('click',function(){
                that.hide();
            });
        }
    },
    /**
     * 显示
     */
    show: function() {
        this.popout.show(200);
        lock_touchmove();
        this.setPosition();
    },
    /**
     * 设置内容
     * @param value 内容
     */
    setContent: function(value) {
        var html = '';
        if(this.whetherClose){
            html += '<div class="closePopout '+(this.title?'small':'big')+'"></div>';
        }
        $('.uu-popout-bd').html(html + value);
        if(this.whetherClose){
            // 关闭
            var that = this;
            $('.closePopout').on('click', function() {
                that.hide();
            });
        }
        var u = navigator.userAgent;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        if (isiOS) {
            $('#popout .uu-popout input,textarea').on('blur', function() {
                that.setPosition();
            })
        } else {
            $(window).resize(function(){
                that.setPosition();
            })
        }
    },
    /**
     * 隐藏
     */
    hide: function() {
        this.popout.hide(200);
        this.popout.remove();
        un_lock_touchmove();
    },
    /**
     * 设置按钮
     * @param num 按钮数量
     * @param value 按钮内容
     */
    setBtn: function(num, value, color) {
        var that = this;
        var color = typeof(color)!="undefined"?color:'';
        if (num == 1) {
            this.cancelBtn.hide();
            value = value ? value : '确定';
            this.primaryBtn.text(value);
            color && this.primaryBtn.css('color',color);
        } else {
            this.cancelBtn.text(value[0]);
            color && this.cancelBtn.css('color',color[0]);
            this.primaryBtn.text(value[1]);
            color && this.primaryBtn.css('color',color[1]);
        }
        // 关闭
        $('.uu-popout-btn').on('click', function() {
            if (that.whetherClose) {
                that.hide();
            }
        });
    },
    /**
     * 获取主操作按钮
     * @returns {*|jQuery|HTMLElement}
     */
    getPrimaryBtn: function() {
        return $('.uu-popout-btn-primary');
    },
    /**
     * 获取辅助操作按钮
     * @returns {*|jQuery|HTMLElement}
     */
    getCancelBtn: function() {
        return $('.uu-popout-btn-default');
    },
    /**
     * 设置是否可以关闭弹出框
     * @param whether 是否可关闭
     */
    setClose: function(whether) {
        this.whetherClose = whether;
    },
    /**
     * 计算位置
     */
    setPosition: function() {
        var tempH = $('#popout .uu-popout').height();
        var screenH = $(window).height();
        var scrollH = $(window).scrollTop();
        var topH = scrollH + (screenH - tempH) / 2;
        if ($(document).height() - scrollH < screenH) {
           topH = (screenH - tempH) / 2;
        }
        $('#popout .uu-popout').css({
            left: ($(window).width() - $('#popout .uu-popout').width()) / 2,
            top: topH
        })
    }
}